//
//  XJYMTSearchViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/24.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#define  CJ_StatusBarAndNavigationBarHeight  (CJ_iPhoneX ? 88.f : 64.f)
#define CJ_iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)


#import "XJYMTSearchViewController.h"
//controller
#import "XJYMTTeiZiDetailViewController.h"

//view
#import "RBSearchNaviBar.h"
#import "XJYMTTopicsSearchReslultView.h" //帖子搜索结果view


@interface XJYMTSearchViewController ()
@property(nonatomic, strong) RBSearchNaviBar *searchBar;
/**默认hidden = YES*/
@property(nonatomic, strong) XJYMTTopicsSearchReslultView *searchResultView;

@end

@implementation XJYMTSearchViewController

#pragma mark - 一 lazy

- (RBSearchNaviBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [[RBSearchNaviBar alloc] initWithFrame:CGRectMake(0, 20, KWidth, CJ_StatusBarAndNavigationBarHeight)];
        
        XJWeakSelf(self);
        _searchBar.cancelBlock = ^{
            [weakself.navigationController popViewControllerAnimated:YES];
        };
        
        _searchBar.searchBlock = ^(NSString *searchStr) {
            // 搜索接口
            XJLog(@"要搜索的字符串是:%@",searchStr);
            weakself.searchResultView.hidden = NO;
            [weakself.searchResultView requestSearchTopicsWithTitle:searchStr];
        };
    }
    return _searchBar;
}


- (XJYMTTopicsSearchReslultView *)searchResultView {
    if (!_searchResultView) {
        _searchResultView = [[XJYMTTopicsSearchReslultView alloc] initWithFrame:CGRectMake(0, kStatusBarAndNavigationBarHeight+30, KWidth, KHeight - kStatusBarAndNavigationBarHeight-30)];
        
        XJWeakSelf(self);
        _searchResultView.xjymtTopicsSearchResultViewClickCellBlock = ^(XJYMTModel *model) {
            [weakself gotoTieZiDetailVcWithXJYMTModel:model];
        };
    }
    return _searchResultView;
}


- (void)gotoTieZiDetailVcWithXJYMTModel:(XJYMTModel *)model {
    XJYMTTeiZiDetailViewController *tieziDetailVC = [[XJYMTTeiZiDetailViewController alloc] init];
    
    tieziDetailVC.navigationItem.title = model.title;
    
    tieziDetailVC.tieZiId = model.tieZiId;
    tieziDetailVC.type = @"ymt";
    [self.navigationController pushViewController:tieziDetailVC animated:YES];
}



#pragma mark - 二 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.searchBar];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];

    [self.searchBar.searchBar becomeFirstResponder];
    
    self.searchResultView.hidden = YES;
    [self.view addSubview:self.searchResultView];
}


- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.searchResultView.hidden = YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
